package com.karlis.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.karlis.domain.Item;
import com.karlis.service.ItemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/item")
public class ItemController {
	
	protected static Logger logger = Logger.getLogger("controller");
	  
	 @Resource(name="itemService")
	 private ItemService itemService;
	 
	 /**
	     * Retrieves the "Add New Credit Card" page
	     */
	    @RequestMapping(value = "/add", method = RequestMethod.GET)
	    public String getAdd(@RequestParam("id") Integer warehouseId, Model model) {
	     logger.debug("Received request to show add page");
	     
	     // Prepare model object
	     Item item = new Item();
	      
	     // Add to model
	     model.addAttribute("warehouseId", warehouseId);
	     model.addAttribute("itemAttribute", item);
	 
	     // This will resolve to /WEB-INF/jsp/add-credit-card.jsp
	     return "add-item";
	 }
	  
	    /**
	     * Adds a new credit card
	     */
	    @RequestMapping(value = "/add", method = RequestMethod.POST)
	    public String postAdd(@RequestParam("id") Integer warehouseId, 
	              @ModelAttribute("itemAttribute") Item item) {
	  logger.debug("Received request to add new credit card");
	   
	  // Delegate to service
	  itemService.add(warehouseId, item);
	 
	  // Redirect to url
	  return "redirect:/karlis/record/list";
	 }
	     
	     
	    /**
	     * Deletes a credit card
	     */
	    @RequestMapping(value = "/delete", method = RequestMethod.GET)
	    public String getDelete(@RequestParam("id") Integer itemId) {
	     logger.debug("Received request to delete credit card");
	     
	      
	     // Delegate to service
	  itemService.delete(itemId);
	 
	  // Redirect to url
	  return "redirect:/karlis/record/list";
	 }
	    
	    /**
	     * Retrieves the "Edit Existing Credit Card" page
	     */
	    @RequestMapping(value = "/edit", method = RequestMethod.GET)
	    public String getEdit(@RequestParam("pid") Integer warehouseId, 
	      @RequestParam("cid") Integer itemId, Model model) {
	     logger.debug("Received request to show edit page");
	      
	     // Retrieve credit card by id
	     Item existingItem = itemService.get(itemId);
	 
	     // Add to model
	     model.addAttribute("warehouseId", warehouseId);
	     model.addAttribute("itemAttribute", existingItem);
	 
	     // This will resolve to /WEB-INF/jsp/edit-credit-card.jsp
	     return "edit-item";
	 }
	  
	    /**
	     * Edits an existing credit card
	     */
	    @RequestMapping(value = "/edit", method = RequestMethod.POST)
	    public String postEdit(@RequestParam("id") Integer itemId, 
	              @ModelAttribute("itemAttribute") Item item) {
	  logger.debug("Received request to add new credit card");
	   
	  // Assign id
	  item.setId(itemId);
	   
	  // Delegate to service
	  itemService.edit(item);
	 
	  // Redirect to url
	  return "redirect:/karlis/record/list";
	 }

}
